const config = require("./grunt.config.js")
const loadTasks = require('load-grunt-tasks')
module.exports = function (grunt) {
    grunt.initConfig({
        // 清除文件
        clean: {
            main: config.path.temp
        },
        // sass转换
        sass: {
            main: {
                files: [{
                    expand: true,
                    cwd: config.path.src,
                    src: [config.path.styles],
                    dest: config.path.temp,
                    base: config.path.src,
                    ext: '.css'
                }]
            }
        },
        // js转换任务
        babel: {
            options: {
                sourceMap: true,
                presets: ['@babel/preset-env']
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: config.path.src,
                    src: [config.path.scripts],
                    dest: config.path.temp,
                    base: config.path.src,
                    ext: '.js'
                }]
            }
        },
        // 图片压缩，字体拷贝
        imagemin: {
            dist: {
                files: [{
                    expand: true,
                    cwd: config.path.src,
                    src: ['**/*.{png,jpg,jpeg,gif,svg,eot,ttf,woff}'],
                    dest: config.path.temp,
                    base: config.path.src
                }]
            }
        },
        swig_precompile: {
            options: {
                active: '',
                locals: {},
                beautify: {
                    indent_size: 2
                }
            },
            dev: {
                options: {
                    force: false
                },
                expand: true,
                cwd: config.path.src,
                src: "src/index.tpl",
                dest: config.path.temp
            }
        },
        // 复制其他资源文件
        copy: {
            html: {
                files: [
                    {
                        expand: true,
                        cwd: config.path.public,
                        src: ['**'],
                        dest: config.path.temp
                    }
                ]
            }
        }
    })

    loadTasks(grunt)

    grunt.registerTask("compile", ["clean", "sass", "babel", "swig_precompile"])
    // 构建工作
    grunt.registerTask("build", ["compile", "imagemin", "copy"])

}